console.log("课题页面")
var roleMap = {
    0:"管理员",
    1:"院士",
    2:"人才",
    3:"单位",
    4:"主管部门",
    5:"专家"
};
var statusMap={
    1:"启用",
    2:"禁用"
};

var categoryMap = {
    1:"创意设计",
    2:"基础部件",
    3:"整机套装",
    4:"应用配套"
};
var catetorySecondMap={};

var editDomainObj= null;

layui.use(['form', 'code', 'laydate','upload','layer','laytpl','element'], function() {
    var laydate = layui.laydate;
    form = layui.form;
    upload = layui.upload;
    layer = layui.layer;
    laytpl = layui.laytpl;
    element = layui.element;




    layui.code();
    //初始化列表

    function  setSelect(dom,data){
        for(let item in data){
            $(dom).append("<option value='" + item +"'>"+ data[item] +"</option>")
        }
        form.render("select");
    }

    //设置二级分类
    AJAX.query(cgiMain.getDict,
        {type:5},
        function (res) {
            if(res.code=='0000'){
                catetorySecondMap = res.result;
                setSelect($("select[name='secondCategory']"), res.result);
                form.render('select');
                form.on("select(secondCategory)",function (data) {
                    $("select[name='thirdCategory']").html("");
                    setThirdCategorySelect(data.value);
                });
            }
        });

    function setThirdCategorySelect(secondCategory){
        AJAX.query(cgiMain.getDict,
            {type:5,parentId:secondCategory},
            function (res) {
                if(res.code=='0000'){
                    setSelect($("select[name='thirdCategory']"), res.result);
                    if(editDomainObj){
                        $(".thirdCategory").val(editDomainObj.thirdCategory);
                    }
                    form.render('select');

                }
            });
    }




    function getThirdCategory(secondCategory){
        var categoryThird = {};
        $.ajax({
            url: cgiMain.getDict,
            dataType: 'json',
            data: {type:5,parentId:secondCategory},
            xhrFields: {
                withCredentials: true
            },
            contentType: false,
            type: 'GET',
            cache: false,
            timeout: CFG.TIMEOUT,
            async:false,
            success: function(res) {
                if (res.code== '0000') {
                    categoryThird = res.result;
                }

            },
            error: function() {}});

        return categoryThird;
    }

    AJAX.post(cgiMain.academistDomainList,
        {},
        function (res) {

            if(res.code == '0000'){
                $(".domainList").data("domainList",res.result);
                var tpl = $("#domainTpl").html();
                for(var i = 0; i < res.result.length;i++){
                    // res.result[i].categoryName=categoryMap[res.result[i].category];
                    var categoryThirdMap =getThirdCategory(res.result[i].secondCategory);
                    res.result[i].secondCategoryName = catetorySecondMap[res.result[i].secondCategory];
                    res.result[i].thirdCategoryName = categoryThirdMap[res.result[i].thirdCategory];
                    laytpl(tpl).render(res.result[i],function (html) {
                        $(".domainList").append(html);

                        $(".domainList").on("click",'.editDomain',function (elem) {
                            editDomain(elem);
                        });
                        $(".domainList").on("click",'.deleteDomain',function (elem) {
                            deleteDomain(elem);
                        });
                    });
                }
                element.render(".domainList");
                form.render();
            }

        });


    // 添加工作经验
    $(".add_btn").on("click", function() {
        $("#myModal").show();
        $("#myModal input, #myModal textarea").val("");
        editDomainObj = null;
    });

    $("#submitNewDomain").click(function () {
        // 判断必填项--提示--统校验
        var params = {};

        // 集合所有参数
        $("#addNewDomainForm .save_val").each(function() {
            var name = $(this).attr("name");
            var val = $(this).val();
            params[name] = val;
        });
        params.roleId=1;

        var url = cgiMain.academistDomainAdd;
        if(params.id && params.id > 0){
            url = cgiMain.academistDomainEdit;
        }


        AJAX.post(url,
            params,
            function (res) {
                if(res.code=='0000'){
                    layer.msg('保存成功');
                    $("#myModal").hide();
                    window.location.reload();
                }
            });
        return false;
    });

    $("#cancelAddNewDomain").on("click",function () {
        $("#myModal").hide();
        $("#myModal input, #myModal textarea").val("");
    });
    $(".close").on("click",function () {
        $("#myModal").hide();
        $("#myModal input, #myModal textarea").val("");
    });


    function editDomain(dom){

        $("#myModal").show();

        editDomainObj = null;

        var domainId = $(dom.target).attr("data-id");
        var domainList = $(".domainList").data("domainList");

        var academistDomain={};
        for(var i = 0 ; i < domainList.length;i++){
            if(domainList[i].id == domainId){
                academistDomain = domainList[i];
                break;
            }
        }

        editDomainObj = academistDomain;

        $("input[name='id']").val(academistDomain.id);
        $(".secondCategory").val(academistDomain.secondCategory);
        form.render('select');
        setThirdCategorySelect(academistDomain.secondCategory);
        form.render('select');
        form.render();
    }

    function deleteDomain(dom){

        var domainId = $(dom.target).attr("data-id");

        layer.open({
            content: '确定删除吗?'
            ,btn: ['删除', '取消']
            ,yes: function(index, layero){
                //按钮【按钮一】的回调
                AJAX.post(cgiMain.academistDomainDelete,
                    {id:domainId},
                    function (res) {
                        if(res.code=='0000'){
                            layer.msg('删除成功');
                            window.location.reload();
                        }
                    });


            }
            ,btn2: function(index, layero){
                //按钮【按钮二】的回调
            }

            ,cancel: function(){
                //右上角关闭回调
            }
        });


    }

    $(".modal-footer > .layui-btn-primary").on("click",function () {
        $("#myModal").hide();
        $("#myModal input, #myModal textarea").val("");
    });


});
